<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleClapse">||||</div>
        <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false">
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item
                :index="subMenu.url+''"
                v-for="subMenu in item.children"
                :key="subMenu.id">
              <template slot="title">
                <i :class="subMenu.icon"></i>
                <span>{{ subMenu.name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!--显示菜单路由 对用的 组件 -->
        <router-view v-if="isRouterAlive"/>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      isCollapse: false,
      isRouterAlive: true
    };
  },
  methods: {
    //刷新
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      });
    },
    //收起侧边栏
    toggleClapse() {
      this.isCollapse = !this.isCollapse;
    },
    //注销
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },

    //初始化加载菜单  先查询所有菜单
    loadMenus() {
      // let url = "/permission/showAllMenu";
      let userInfo = JSON.parse(sessionStorage.getItem("managerInfo"));
      let url = "/permission/showMenuByManager/"+userInfo.id;
      this.$axios.get(url)
          .then(res => {
            if (res.data.code == 200) {
              this.menuList = res.data.data;
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg,
                duration: 1000
              })
            }
          })
    }
  },
  created() {
    this.loadMenus();
  }
};
</script>
<style lang="less" scoped>
.toggle-button {
  background-color: #373d48;
  font-size: 10px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 22px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 14px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}
</style>